<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataContentCompose extends AdminBase
{
    public function index()
    {
        $param = $this->request->param();
        $db = Db::name('data_content_compose'); 
        if(!empty($param['parent_typeid'])) {
            $db = $db->where('parent_typeid',$param['parent_typeid']);
            if(!empty($param['typeid'])){
                $db = $db->where('typeid',$param['typeid']);
            }
        }
        if(!empty($param['kw'])){
            $db = $db->where('title','like','%'.$param['kw'].'%');
        }
        $list = $db->order('id desc')->paginate(['list_rows'=> 100,'query' => $param]);
        $count = $list->total();
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $param['parent_typeid']
        ])->select()->toArray();
        return view('',['list'=>$list,'count'=>$count,'param'=>$param,'parent_typelist'=>$parent_typelist,'typelist'=>$typelist]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if(!empty($param['parent_typeid'])) $param['top_typeid'] = get_typepid($param['parent_typeid']);
            $result = Db::name('data_content_compose')->insert($param);
            if( $result ) {
                xn_add_admin_log('添加原创数据');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        return view('form',[
            'parent_typelist'=>$parent_typelist,
        ]);
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if(!empty($param['parent_typeid'])) $param['top_typeid'] = get_typepid($param['parent_typeid']);
            $result = Db::name('data_content_compose')->update($param);
            if( $result ) {
                xn_add_admin_log('修改原创数据');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $data = Db::name('data_content_compose')->find($id);
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $data['parent_typeid']
        ])->select()->toArray();

        return view('form',[
            'data'=>$data,
            'parent_typelist'=>$parent_typelist,
            'typelist'=>$typelist,
        ]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name('data_content_compose')->delete($id);
        xn_add_admin_log('删除原创数据');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        Db::name('data_content_compose')->delete($ids);
        xn_add_admin_log('批量删除原创数据');
        $this->success("删除成功");
    }

    public function import(){
        $file = request()->file('file');
        $savename = \think\facade\Filesystem::putFileAs( 'excel', $file, 'compose.xlsx');
        $filePath = \think\facade\Filesystem::path($savename);

        // $filePath = 'D:\phpstudy_pro\WWW\www.xnadmin.site\runtime\storage\excel\compose.xlsx';
        if(!file_exists($filePath)){
            return json(['status'=>'0','msg'=>'文件不存在!']);
        }

        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if(!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if(!$PHPReader->canRead($filePath)) {
                return json(['status'=>'0','msg'=>'No Excel!']);
            }
        }

        $PHPExcel = $PHPReader->load($filePath);
        $currentSheet = $PHPExcel->getSheet(0);
        //$allColumn = $currentSheet->getHighestColumn();
        $allRow = $currentSheet->getHighestRow();

        $limitNum = 10;
        $limitPage = ceil($allRow / $limitNum);
        $count = 0;
        $make = new \app\common\lib\MakeCompose();
        for($i=0;$i<$limitPage;$i++){
            $limitIndex = $i * $limitNum;
            $limitEnd = $limitIndex + $limitNum;
            if($i == 0){
                $limitIndex = 2;
            }
            if($limitEnd >= $allRow){
                $limitEnd = $allRow+1;
            }
            for($currentRow = $limitIndex; $currentRow < $limitEnd; $currentRow++) {
                $make->dalei = $currentSheet->getCell('A'.$currentRow)->getValue();
                $make->xiaolei = $currentSheet->getCell('B'.$currentRow)->getValue();
                $make->xiangmu = $currentSheet->getCell('C'.$currentRow)->getValue();
                $make->keywords = $currentSheet->getCell('D'.$currentRow)->getValue();
                $make->keywords_type = $currentSheet->getCell('E'.$currentRow)->getValue();
                if(is_object($make->dalei)) $make->dalei = $make->dalei->__toString();
                if(is_object($make->xiaolei)) $make->xiaolei = $make->xiaolei->__toString();
                if(is_object($make->xiangmu)) $make->xiangmu = $make->xiangmu->__toString();
                if(is_object($make->keywords)) $make->keywords = $make->keywords->__toString();
                if(is_object($make->keywords_type)) $make->keywords_type = $make->keywords_type->__toString();
                if($cc->keywords == ''){
                    continue;
                }
                $data = $make->run();
                $r = Db::name('data_content_compose')->insert($data);
                if($r){
                    $count++;
                }
            }
        }
        return json(['status'=>'1','msg'=>'成功导入 '.$count.' 条数据']);

    }

}
